package csc.fresher.view;

import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.EncryptMD5;
import csc.fresher.entity.SystemUser;
import csc.fresher.service.SystemLoginService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemLoginManageBean implements Serializable {
	private static final long serialVersionUID = 3325780045480163859L;
	private String userName;
	private String password;
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	public static Timestamp beginTime;
	public List<Long> roles;

	private SystemUser systemUser = null;
	
	@Autowired
	private SystemLoginService loginService;

	@Autowired 
	private SystemUserService systemUserService;

	public String login() {
		if ("".endsWith(userName) || "".endsWith(password)) {
			System.out.println(EncryptMD5.md5(password));
			return "Logon";	
		} else {
			systemUser = loginService.getSystemUser(userName, password);
			FacesContext context = FacesContext.getCurrentInstance();

			if (systemUser != null) {
				context.getExternalContext().getSessionMap().put("userSS", systemUser);
				beginTime = new Timestamp(System.currentTimeMillis());
				List<Long> listrole = systemUserService.getRoleByUserName(userName);
				roles = systemUserService.getRoleByUserName(userName);
				return "UserManagement";
			} else {
				FacesContext.getCurrentInstance();
				context.addMessage(
						null,
						new FacesMessage(
								"Your Email Address or Password is wrong, please try again!"));		
			}
		}
		return "Logon";	
	}
	
	
	
	
	public String getUserName() {

		return userName;

	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {

		return password;

	}

	public void setPassword(String password) {
		this.password = password;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public SystemLoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(SystemLoginService loginService) {
		this.loginService = loginService;
	}



}
